<template>
  <div class="search">
    <div class="title">
      <div class="back">
        <van-icon name="arrow-left" class="size" @click="gotoback" />
      </div>
      <van-search
        v-model="keyword"
        class="btn"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
      >
      </van-search>
    </div>
    <div class="tui" v-if="judge">
      <h2>热门推荐</h2>
      <ul>
        <li class="tui-content" v-for="(l, i) in a" :key="i">
          <router-link :to="{ name: 'good', query: { skuId: l.skuId } }">{{
            l.spuName
          }}</router-link>
        </li>
      </ul>
    </div>
    <div v-else class="search2">
      <div v-if="list.length > 0">
        <div class="sea" v-for="(l, i) in list" :key="i">
          <router-link :to="{ name: 'good', query: { skuId: l.skuId } }">
            <div class="sea-left">
              <img :src="l.mainImg" alt="" />
            </div>

            <div class="sea-right">
              <h2>{{ l.spuName }}</h2>
              <p>{{ l.spuShortDesc }}</p>
              <span>￥{{ l.price }}</span>
            </div>
          </router-link>
        </div>
      </div>

      <div class="empty" v-else>
        <van-empty
          image="https://dsfs.oppo.com/store/cn/m/img/noProduct.9761300b.png"
          description="没有找到相关的产品"
        />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      keyword: "",
      a: [],
      judge: true,
      list: [],
      content: [],
    };
  },
  methods: {
    onSearch(value) {
      console.log(value);
      this.judge = false;
      this.list = this.content.filter((l) => {
        return (
          l.spuName.indexOf(value) > -1 || l.spuShortDesc.indexOf(value) > -1
        );
      });
      console.log(this.list);
    },
    onCancel() {
      this.judge = true;
      this.value = "";
      this.list = [];
    },
    async getOPcontent() {
      let res = await this.$ajax.findGoods();
      this.a = res.slice(0, 6);
      this.content = res;
      console.log(this.content);
    },
    // async setHost(){
    //     let res=await this.$ajax.findGoods()
    // }
  },
  mounted() {
    this.getOPcontent();
  },
};
</script>

<style lang="scss" scoped>
.search {
  width: 100%;
  background: #f7f8fa;
  //   position: relative;
  .title {
    position: fixed;
    top: 0;
    left: 0;
    padding-bottom: 50px;
    .back {
      float: left;
      padding-top: 13px;
      padding-left: 10px;
      .size {
        font-size: 26px;
      }
    }
    .btn {
      width: 320px;
    }
  }
  .tui {
    margin-top: 50px;
    margin-left: 10px;
    min-height: 500px;
    h2 {
      padding-top: 30px;
      font-size: 16px;
      color: #999;
    }
    .tui-content {
      float: left;
      font-weight: 400;
      font-size: 15px;
      margin-top: 20px;
      margin-right: 10px;
      margin-bottom: 10px;
      padding: 5px 5px;
      line-height: 20px;
      border-radius: 10px;
      color: #fff;
      background: -webkit-linear-gradient(222deg, #ff4343, #ff8243);
    }
  }
  .search2 {
    width: 100%;
    margin-top: 60px;
    .sea {
      width: 100%;
      padding: 10px 10px;
      display: flex;
      background: #fff;
      margin-top: 10px;
      border-radius: 2px;
      a {
        text-decoration: none;

        .sea-left {
          float: left;
          img {
            width: 100px;
            height: 100px;
          }
        }
        .sea-right {
          padding-top: 18px;
          padding-left: 20px;
          float: left;
          h2 {
            font-size: 16px;
            color: #000;
          }
          p {
            font-size: 14px;
            color: #999;
          }
          span {
            font-size: 18px;
            color: red;
            font-weight: bold;
          }
        }
      }
    }
  }
}
</style>